package com.lopo.util;


import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.*;

/**
 * Created by MyWin on 2017/7/7.
 */
public class TbitDiffUtil {
    static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(TbitDiffUtil.class);
    private static final String PATH_TEMP_FILE_DIR = "tempfile";
    private static final String PATH_TOOL_BIN_DIR = "bin";
    private static final String FILE_DIFF_TOOL_NAME = "TbitDiff_R";
    private static Runtime runtime;
    private static String root;
    private static String tempPath;
    private static String binPath;

    static {
        runtime = Runtime.getRuntime();
        root = TbitDiffUtil.class.getResource("/").getPath();
        tempPath = String.format("%s%s", root, PATH_TEMP_FILE_DIR);
        binPath = String.format("%s%s/%s", root, PATH_TOOL_BIN_DIR, FILE_DIFF_TOOL_NAME);
        // 检测目录是否存在不然就创建
        File formFile = new File(tempPath);
        if (!formFile.exists()) {
            formFile.mkdir();
        }
    }


    /**
     * 获取差分文件
     *
     * @param fromFirm
     * @param toFirm
     * @return
     */
    public static byte[] getDiffFile(byte[] fromFirm, byte[] toFirm) {
        String fromFileName ="from";
        String toFileName = "to";

        String fromFilePath = String.format("%s/%s", tempPath, fromFileName);
        String toFilePath = String.format("%s/%s", tempPath, toFileName);
        String diffFilePath = String.format("%s/%s.%s.out", tempPath, fromFileName, toFileName);

        String cmdOrder;
        if (OsUtil.isLinux()) {
            cmdOrder = String.format("./%s_L %s %s %s", FILE_DIFF_TOOL_NAME, fromFilePath, toFilePath, diffFilePath);
        } else {
            cmdOrder = String.format("%s %s %s %s", binPath, fromFilePath.substring(1), toFilePath.substring(1), diffFilePath.substring(1));
        }

        File formFile = new File(fromFilePath);
        writeFile(formFile, fromFirm);
        File toFile = new File(toFilePath);
        writeFile(toFile, toFirm);
        File diffFile = new File(diffFilePath);
        try {
            if (OsUtil.isLinux()) {
                runtime.exec(cmdOrder, null, new File(String.format("%s%s", root, PATH_TOOL_BIN_DIR))).waitFor();
            } else {
                runtime.exec(cmdOrder).waitFor();
            }
            return readFile(diffFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void writeFile(File file, byte[] data) {
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            FileOutputStream in = null;
            try {
                in = new FileOutputStream(file);
                try {
                    in.write(data);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public static byte[] readFile(File file) {
        byte[] data = null;
        if (file.exists()) {
            data = new byte[(int) file.length()];
            InputStream in = null;
            try {
                in = new FileInputStream(file);
                in.read(data);
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return data;
    }
}
